# coding=utf-8
from selenium import webdriver
from pyquery import PyQuery as pq
import time
import os
import json
# 配置信息
config = {}
def readConfig(cPath):
    con = json.load(open(cPath,encoding='utf-8'))
    return con

def init_PhantomJS(pPath):
    service_args=[]
    service_args.append('--load-images=no')  ##关闭图片加载
    service_args.append('--disk-cache=no')  ##开启缓存
    service_args.append('--ignore-ssl-errors=true') ##忽略https错误
    driver = webdriver.PhantomJS(executable_path=pPath,service_args=service_args)
    driver.maximize_window()
    return driver

def login_QQ(driver,qqnum,password):
    # 判断是否登录页
    try :
        driver.find_element_by_id('login_div')
        a = True
    except:
        a = False
    if a==False :  # 如果不是登录页 返回login失败
        print('is not login page!')
        return False
    # 如果是登录页 开始模拟登录
    try:
        driver.switch_to.frame('login_frame')            #切换到登录ifram
        time.sleep(2)
        driver.find_element_by_id('switcher_plogin').click()
        time.sleep(2)
        driver.find_element_by_id('u').clear()           #选择用户名框
        driver.find_element_by_id('u').send_keys(qqnum)
        driver.find_element_by_id('p').clear()           #选择密码框
        driver.find_element_by_id('p').send_keys(password)
        driver.find_element_by_id('login_button').click()   #点击登录按钮
    except :
        driver.save_screenshot('./error.jpg')
        return False
    time.sleep(3)
    # 判断好友是否有权限 
    try:
        driver.find_element_by_id('QM_OwnerInfo_Icon')
        b = True
    except:
        b = False
    if b==False :
        print('not acc')
        driver.save_screenshot('./noACC.jpg')
        return False
    else :
        return driver
def anaDat(htmlText):
    resDat = []
    dom = pq(htmlText)
    dom = dom("ol#msgList")
    msgList = dom('div.bgr3')
    for oness in msgList.items():
        oneDat = {}
        dom = pq(oness)
        oneDat['text'] = dom('div.bd').text()
        stime = dom('span.c_tx3')
        stime = stime('a').attr('title')
        oneDat['stime'] = stime
        # 解析图片
        # pic = dom('div.md')
        # if pic != False:
        #     urls =[]
        #     pic = pic('img')
        #     for one in pic.items():
        #         urls.append(one.attr('src'))
        #     oneDat['picUrl'] = urls
        resDat.append(oneDat)
    return resDat
def save_data(data,path):
    json.dump(data, open(path,'w',encoding='utf-8'), ensure_ascii=False, indent=4)
    return True
def start_get_suosuo(youQQ,password,mdQQ):
    count_ss = 0
    Dr = init_PhantomJS(config['PhantomJS']) #初始化浏览器
    QQzerourl = config["QQzerourl"]+mdQQ+'/311' 
    Dr.get(QQzerourl)
    Dr = login_QQ(Dr,youQQ,password) #登录qq
    if Dr == False : 
        print('error:cannot login QQ')
        return False 
    # 切换到说说内容框
    Dr.switch_to.frame('app_canvas_frame')
    while True :
        # 判断是否有内容
        try:
            Dr.find_element_by_css_selector("img[class=\"empty_pic_message\"]")
            break
        except :
            pass
        htmlText = Dr.page_source
        # with open('./temp.html','r',encoding="utf-8") as fp : htmlText = fp.read()    
        resDat = anaDat(htmlText)       # 分析数据
        fName = '/suosuo[qq=%s](%d-%d).json'%(mdQQ,count_ss,count_ss+len(resDat))
        if not os.path.isdir(config['save']):
            os.makedirs(config['save'])
        save_data(resDat,config['save']+fName)         # 保存数据
        Dr.save_screenshot(config['save']+fName[:-5]+'.jpg')
        count_ss += len(resDat)
        nowUrl = Dr.current_url
        # 进度信息
        print("【%s】: %s \n\t...(+%d)  All saved: %d"%(resDat[0]['stime'],resDat[0]['text'],len(resDat),count_ss))
        Dr.find_element_by_css_selector("a[class=\"c_tx\"][title=\"下一页\"]").click()
        time.sleep(3)
    Dr.close()
    Dr.quit()
    
if not os.path.exists('./config.json') : 
    input("程序已损坏【配置文件丢失】")
    exit(0)
config =  readConfig('./config.json')

info = '''
欢迎使用QQ说说爬取器(v1.1)    by:smallcatx0
    说明：获取QQ好友的所有可见说说。
        结果在data目录下的.json 文件里
        用记事本即可打开
'''
print(info)
while True:
    if "mdQQ" in config.keys():
        print('正在根据您配置文件进行爬取！')
        youQQ = config['mdQQ']['youQQ']
        password = config['mdQQ']['password']
        QQs = config['mdQQ']["QQs"]
        tReadConfig =True
        break
    tReadConfig = False
    temp = input("请输入你要获取说说的好友的QQ：（如果有多个请用空格隔开）\n")
    QQs = temp.split(' ')
    print("你要爬取的好友QQ号如下：",QQs,len(QQs))
    key = input("请确认(y/n):")
    if key in "yY": break
if not tReadConfig:
    youQQ = input("登录qq:")
    password = input("密码:")
print("为了确保成功每一个页面加载都需要3-5s 请耐心等待")

for oneQQ in QQs:
    start_get_suosuo(youQQ,password,oneQQ)